package org.wordpress.android.dashboard;

import java.util.LinkedList;
import java.util.List;

import org.wordpress.android.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DashboardEventAdapter extends ArrayAdapter<DashboardEvent> {

    private List<DashboardEvent> events;
    private int imgViewResourceId;
    private int textViewResourceId;

    public DashboardEventAdapter(Context context) {
        super(context, R.layout.dashboard, R.id.eventText);
        this.events = new LinkedList<DashboardEvent>();
        this.imgViewResourceId = R.id.eventImg;
        this.textViewResourceId = R.id.eventText;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        DashboardEvent item = getItem(position);

        TextView text = (TextView) view.findViewById(this.textViewResourceId);
        text.setText(item.getLabel());

        ImageView imgView = (ImageView) view.findViewById(this.imgViewResourceId);
        imgView.setImageResource(item.getType().getImageResource());

        return view;
    }

    public void setEvents(List<DashboardEvent> newEvents) {
        synchronized (this.events) {
            this.events.clear();
            this.events.addAll(newEvents);
        }
    }
}
